package com.weita.lightfood.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author IT_Monster at 2019/04/20 17:01.
 * 处理时间工具
 */
public class DateUtil {

    /**
     * 通过出生日期计算年龄
     * @param birth
     * @return
     */
    public static int getAge(String birth) {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formatNowDate = simpleDateFormat.format(nowDate);

        String[] birthDateString = birth.split("-");
        String[] nowDateString = formatNowDate.split("-");

        int birthYear = Integer.parseInt(birthDateString[0]);
        int nowYear = Integer.parseInt(nowDateString[0]);
        return nowYear - birthYear;
    }


    /**
     * 计算是否超时
     * @param endTime
     * @return
     */
    public static boolean isEnd(String endTime) {
        Date nowTime = new Date();
        DateFormat timeInstance = DateFormat.getTimeInstance();
        final String nowTimeString = timeInstance.format(nowTime);
        if (nowTimeString.compareTo(endTime) < 0) {
            //没超时
            return false;
        }
        return true;
    }


    /**
     * 获取当前时间年月日
     * @return
     */
    public static String nowDate() {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formatNowDate = simpleDateFormat.format(nowDate);
        return formatNowDate;
    }

    /**
     * 获取当前时分秒
     * @return
     */
    public static String nowTime(){
        Date nowTime = new Date();
        DateFormat timeInstance = DateFormat.getTimeInstance();
        final String nowTimeString = timeInstance.format(nowTime);
        return nowTimeString;
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.isEnd("17:00:00"));
    }
}
